package event.gui;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JWindow;
import javax.swing.Timer;

import main.model.RFIDCardReader;
import event.model.NarwhalEvent;

@SuppressWarnings("serial")
public class EventPopUp extends JWindow implements ActionListener {

	private final int WAIT_FOR_CARD = 0;
	private final int CARD_NOT_FOUND = 1;
	private final int REGISTERED = 2;
	private final int ALREADY_REGISTERED = 3;
	private final int EVENT_LOCKED = 4;
	private final int EVENT_FULL = 5;
	private final int FUNK_ONLY = 6;
	private final int NO_CARD_READER = 7;
	
	private EventFrame frame;
	private NarwhalEvent model;
	private int state;
	private Image[] states;
	private Timer t;
	
	public EventPopUp(NarwhalEvent model, EventFrame frame) {
		this.frame = frame;
		this.model = model;
		frame.setEnabled(false);
		
		states = new Image[8];
		states[WAIT_FOR_CARD] = new ImageIcon("img/panther_blipp.jpg").getImage();
		states[EVENT_LOCKED] = new ImageIcon("img/panther_closed.png").getImage();
		states[CARD_NOT_FOUND] = new ImageIcon("img/panther_no_card.gif").getImage();
		states[REGISTERED] = new ImageIcon("img/panther_done.jpg").getImage();
		states[ALREADY_REGISTERED] = new ImageIcon("img/panther_already_reg.jpg").getImage();
		states[EVENT_FULL] = new ImageIcon("img/panther_full.jpg").getImage();
		states[FUNK_ONLY] = new ImageIcon("img/panther_funk.jpg").getImage();
		states[NO_CARD_READER] = new ImageIcon("img/no_reader.jpg").getImage();
		
		setLocation(312, 184);
		setSize(400, 400);
		setVisible(true);

		if (model.isActiveClosed()) {
			state = EVENT_LOCKED;
			shutDown();
		} else if (!model.placesLeft()) {
			state = EVENT_FULL;
			shutDown();
		} else {		
			new CardReader();
		}
		
	}
	
	private void update() {
		switch (model.register()) {
		case NarwhalEvent.REGISTERED:
			state = REGISTERED;
			break;
		case NarwhalEvent.EVENT_FULL:
			state = EVENT_FULL;
			break;
		case NarwhalEvent.NO_USER:
			state = CARD_NOT_FOUND;
			break;
		case NarwhalEvent.ALREADY_REGISTERED:
			state = ALREADY_REGISTERED;
			break;
		case NarwhalEvent.EVENT_ONLY_FUNK:
			state = FUNK_ONLY;
			break;
		}
		shutDown();
	}
	
	private void shutDown() {
		System.out.println("Ska stänga ner");
		repaint();
		t = new Timer(3000,this);
		frame.moveMouse();
		t.start();
	}

	private void closeWindow() {
		System.out.println("Stänger rutan");
		t.stop();
		frame.setEnabled(true);
		model.clearBlipp();
		dispose();
	}

	@Override
	public void paint(Graphics g) {
		g.drawImage(states[state],0,0,null);
		g.setFont(new Font("Ume Gothic",Font.PLAIN,42));
		if (state == REGISTERED) {
			FontMetrics fm = g.getFontMetrics();
			String name = model.getBlippedFirstName();
			int x = 270-fm.stringWidth(name)/2;
			g.drawString(name, x, 265);			
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		closeWindow();
	}

	private class CardReader extends Thread {
		public CardReader() {
			start();
		}

		@Override
		public void run() {
			String card = null;
			try {
				card = RFIDCardReader.createRFID().waitAndGetCard();
			} catch (IllegalArgumentException e) {
				System.err.println("No card reader found!");
				state = NO_CARD_READER;
				shutDown();
				return;
			}
			System.out.println(card);
			if (card == null) {
				closeWindow();
				return;
			}
			model.setCard(card);
			update();
		}
	}
}
